#include "login.h"
#include "ui_login.h"
#include "signup.h"
#include "mainwindow.h"
#include "comsignup.h"
#include <QSqlDatabase> //数据驱动
#include <QSqlQuery> //数据库执行语句
#include <QSqlRecord>
#include <QMessageBox>
#include <QGraphicsDropShadowEffect>
#include <QDebug>

Login::Login(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Login)
{
    ui->setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint);
    //初始化
    registerFlag = true;

    //设置图片
    QPixmap *pix = new QPixmap(":/images/in2.png");
    QSize sz = ui->label_image->size();
    ui->label_image->setPixmap(pix->scaled(sz));
    QPixmap *pix2 = new QPixmap(":/images/cuteIcon3.ico");
    QSize sz2 = ui->label_icon->size();
    ui->label_icon->setPixmap(pix2->scaled(sz2));
    QPixmap *pix3 = new QPixmap(":/images/tree3.png");
    QSize sz3 = ui->label_image2->size();
    ui->label_image2->setPixmap(pix3->scaled(sz3));
    //设置登录界面的背景图
    this->setFixedSize(800,540);//固定窗口大小
    QPalette pal =this->palette();
    QImage ImgAllbackground(":/images/background1.jpg");
    QImage fitimpic = ImgAllbackground.scaled(this->width(),this->height(),Qt::IgnoreAspectRatio);
    pal.setBrush(QPalette::Window,QBrush(fitimpic));
    setPalette(pal);

    init();
//    //设置图片阴影效果
//    QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
//    shadow->setOffset(-3, 0);
//    shadow->setColor(QColor("#888888"));
//    shadow->setBlurRadius(30);
//    ui->label_image->setGraphicsEffect(shadow);

}

void Login::init(){
    if(registerFlag)
        accept();
}

Login::~Login()
{
    delete ui;
}

//数据库初始化
int Login::mysql_init(){
    QSqlDatabase db;
    QString dsn;
    //先判断是否已经连接
    if(QSqlDatabase::contains("qt_sql_default_connection"))
      db = QSqlDatabase::database("qt_sql_default_connection");
    //没有则连接自己的数据库
    else{
      db = QSqlDatabase::addDatabase("QODBC");
      dsn = "Driver={MySQL ODBC 8.0 UniCode Driver};"; // Driver is now {MySQL}
//          dsn.append("Server=192.168.112.132,3306;"); // IP,Port
          //dsn.append("Server=192.168.0.196,3306;"); // IP,Port
          dsn.append("Server=127.0.0.1,3306;"); // IP,Port
          dsn.append("Database=calculate24;"); // Schema
          dsn.append("Uid=root;"); // User
          dsn.append("Pwd=1234;"); // Password
          dsn.append("MULTI_HOST=1;");
          db.setDatabaseName(dsn);
    }
    if(!db.open())
    {
        qDebug()<<"open error";
        QMessageBox::information(this,"404","失败!绝对不是管理员的问题!");
        return 0;
    }
    else{
        qDebug()<<"table create success";
        return 1;
    }
}

void Login::on_closeBtn_clicked()
{
    close();
}

void Login::on_sign_in_clicked()
{
    //如果登录失败，结束函数
    if(mysql_init()==0){
        return;
    }
    else {
        QString username = ui->le_username->text();
        QString password = ui->le_password->text();
        QString sql=QString("select * from tb_user where username='%1' and password='%2'")
                    .arg(username).arg(password);
        //创建执行语句对象
        QSqlQuery query(sql);
        //得到查询数据
        QSqlRecord rec = query.record();
        //判断执行结果
        if(!query.next()){
            qDebug()<<"Login error";
            QMessageBox::information(this,"登录认证","登录失败,账户或者密码错误");
        }
        else
        {
            qDebug()<<"Login success";
            QMessageBox::information(this,"登录认证","登录成功");
            rec = query.record();
            int usernameCol = rec.indexOf("username");
            int highestScoreCol = rec.indexOf("highestScore");
            QString usernameValue = query.value(usernameCol).toString();
            QString highestScoreValue = query.value(highestScoreCol).toString();
            qDebug() << usernameValue << highestScoreValue;
            emit comToMain(usernameValue,highestScoreValue);
            //登录成功后可以跳转到其他页面
            accept();
         }
    }
}

void Login::on_sign_up_clicked()
{
//    s->show();
//    this->hide();
//    SignUp s;
//    s.exec();
    registerFlag = false;
    this->hide();
    comSignUp::p_signUp->exec();
}

void Login::on_normal_login_clicked()
{
    QMessageBox::information(this,"登录认证","游客登录成功!");
    accept();
}
